const api = require("../../utils/cloudAPIUtils");
const app = getApp()

Page({
  data:{
    title:app.globalData.title
  },

  toIndex(){
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },

  /**
   * 根据路径，去到对应的页面
   */
  onLoad(options){
    console.log("=====引导页得到路径：" + options.q);
    if(!options.q){
        this.toIndex();
        return;
    }
    let urls = options.q.replace(/%2F/g,'/');
    let path = urls.split("/");
    console.log(urls);
    console.log(path);

    //登录逻辑
    if (!app.globalData.userInfo.loginToken) {
        api.login((res) => {
            this.handlePath(path)
        }, (res) => {
            this.toIndex();
        })
    }else{
        this.handlePath(path)
    }
  },

  // 4:设备类型  5：设备序列号
  handlePath(path){
    if(path[4]=='cdcz' || path[4]=='cdb' || path[4]=='pcl' || path[4]=='bxscn'){
        console.log("设备号："+path[5]);
        if(path[4] == 'bxscn'){
            wx.reLaunch({
                url: `/packageA/pages/bxscn/lease/index?deviceSn=${path[5]}`
            })
        }else{
            wx.reLaunch({
                url: `/packageA/pages/${path[4]}/index/index?deviceSn=${path[5]}`
            })
        }
    }else{
        wx.showModal({
            title:'二维码错误',
            content:'请重新扫描正确的二维码',
            showCancel:false,
            success(){
                wx.reLaunch({
                    url: "/pages/index/index",
                })
            }
        })
    }
  },
})
